/*
 * CS_REQ_WORLD_BOSS_RANK_REWORD.cpp
 *
 *  Created on: 2014. 1. 13.
 *      Author: jjaehuny
 */


#include "../../Command.h"

#include "../../User.h"
#include "../../NLogic/Space.h"
#include "../../NResource/Static.h"

#include <sstream>

namespace ProjectZ { namespace NCommand {

static void SendAck_CS_REQ_WORLD_BOSS_RANK_REWORD(User * pUser, int16_t ack)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_REQ_WORLD_BOSS_RANK_REWORD);
	pUser->Send(pAck);
}

static bool sendGold(User * pUser, int gold)
{
	NLogic::MailBox::Mail::Data clsMailData;
	clsMailData.send_user_seq		= 0;
	clsMailData.send_user_nickname	= "GAMEVIL";
	clsMailData.recv_user_seq		= pUser->GetUserSeq();
	clsMailData.message				= NResource::Static::InstancePtr()->GetMailMessageResource()->GetMessageWorldBossReward();
	clsMailData.gift_status			= EM_GIFT_GOLD;
	clsMailData.gold					= gold;

	// 메일 발송
	if (false == NLogic::NFunction::SendMail(pUser, clsMailData))
	{
		DEBUGLOG("SendConnectEventReward : GOLD & ZEN Mail Send Fail!!");
		return false;
	}

	return true;
}

static bool sendItem(User * pUser, int tid, int class_tye, int sub_type)
{
	NLogic::Item::Data clsData;
	clsData.tid = tid;
	clsData.quantity = 1;
	clsData.class_type = class_tye;
	clsData.sub_type = sub_type;
	NResource::Static::InstancePtr()->GetItemResource()->SetItemDataFromResource(pUser, clsData);
	NResource::Static::InstancePtr()->GetItemResource()->setItemEffect(clsData);

	NLogic::MailBox::Mail::Data clsMailData;
	clsMailData.send_user_seq		= 0;
	clsMailData.send_user_nickname	= "GAMEVIL";
	clsMailData.recv_user_seq		= pUser->GetUserSeq();
	clsMailData.message				= NResource::Static::InstancePtr()->GetMailMessageResource()->GetMessageWorldBossReward();
	clsMailData.gift_status			= EM_GIFT_ITEM;
	clsMailData.item					= clsData;

	if (false == NLogic::NFunction::SendMail(pUser, clsMailData))
	{
		return false;
	}

	return true;
}

static bool send_main_Reward1(User * pUser, NResource::WorldBossReward::Data & clsRewardData)
{
	/*
	0 : 아이템
	1 : 골드
	2 : 없음
	*/

	// 골드 발송
	if (1 == clsRewardData.reward1_type)
	{
		sendGold(pUser, clsRewardData.reward1_value);
		return true;
	}

	// 아이템 발송
	if (0 == clsRewardData.reward1_type)
	{
		for ( int i = 0; i < clsRewardData.reward1_value; i++)
		{
			sendItem(pUser, clsRewardData.reward1_tid, clsRewardData.reward1_class_type, clsRewardData.reward1_sheet_type);
		}
	}

	return false;
}

static bool send_main_Reward2(User * pUser, NResource::WorldBossReward::Data & clsRewardData)
{
	/*
	0 : 아이템
	1 : 골드
	2 : 없음
	*/

	// 골드 발송
	if (1 == clsRewardData.reward2_type)
	{
		sendGold(pUser, clsRewardData.reward2_value);
		return true;
	}

	// 아이템 발송
	if (0 == clsRewardData.reward2_type)
	{
		for ( int i = 0; i < clsRewardData.reward2_value; i++)
		{
			sendItem(pUser, clsRewardData.reward2_tid, clsRewardData.reward2_class_type, clsRewardData.reward2_sheet_type);
		}
	}

	return false;
}

void Command::CS_REQ_WORLD_BOSS_RANK_REWORD(User * pUser, ::Network::Packet * pPacket)
{
	/*
	/// 월드보스 보상
	CS_REQ_WORLD_BOSS_RANK_REWARD		0x4013
	{

	}
	SC_REQ_WORLD_BOSS_RANK_REWARD		0x4014
	{
		s16 sAck;
	}
	*/

	NLogic::Space * pWorld = pUser->GetSpace();
	if (NULL == pWorld)
	{
		DEBUGLOG("%s NULL == pWorld", __FUNCTION__);
		SendAck_CS_REQ_WORLD_BOSS_RANK_REWORD(pUser, ACK_NO_SPACE);
		return;
	}

	if (pWorld->GetData()._spaceType != NLogic::Space::SpaceType::WORLD)
	{
		DEBUGLOG("%s pWorld->GetData()._spaceType != NLogic::Space::SpaceType::WORLD", __FUNCTION__);
		SendAck_CS_REQ_WORLD_BOSS_RANK_REWORD(pUser, ACK_NO_SPACE);
		return;
	}

	if (pUser->GetEnumState() != User::EnumState::__NONE__)
	{
		DEBUGLOG("%s pUser->GetEnumState() != User::EnumState::__NONE__", __FUNCTION__);
		SendAck_CS_REQ_WORLD_BOSS_RANK_REWORD(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NResource::EventTimeResource * pEventTimeResource = NResource::Static::Instance().GetEventTime();
	if (NULL == pEventTimeResource)
	{
		DEBUGLOG("%s NULL == pEventTimeResource", __FUNCTION__);
		SendAck_CS_REQ_WORLD_BOSS_RANK_REWORD(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	if (false == pEventTimeResource->IsWorldBossReward(pUser))
	{
		DEBUGLOG("%s false == pEventTimeResource->IsWorldBossReward(pUser)", __FUNCTION__);
		SendAck_CS_REQ_WORLD_BOSS_RANK_REWORD(pUser, ACK_WORDBOSS_NOT_EVENT);
		return;
	}

	int rank = -1;
	{
		::NoSQL::Command::Redis clsRedis;
		clsRedis._eServer = ::NoSQL::Command::BaseInfo::REDIS;
		clsRedis._eAction = ::NoSQL::Command::Redis::ZREVRANK;
		clsRedis._key = "RANK";
		std::stringstream stream;
		stream << pUser->GetUserSeq();
		clsRedis._value = stream.str();

		::NoSQL::Command::Auto clsAuto(::NoSQL::GETCOMMANDPOOL());
		clsAuto->SetRedis(clsRedis);

		::NoSQL::SESSION()->SyncExecute(static_cast<int>(RedisServerType::WORLDBOSS_MASTER), clsAuto);

		if (0 == clsAuto->GetResult())
		{
			DEBUGLOG("ZREVRANK RANK : %d", clsAuto->GetRedis()._outRank);
			rank = clsAuto->GetRedis()._outRank;
		}
		else
		{
			DEBUGLOG("ZRANK RESULT : %d", clsAuto->GetResult());
		}
	}

	if (-1 == rank)
	{
		DEBUGLOG("%s -1 == rank", __FUNCTION__);
		SendAck_CS_REQ_WORLD_BOSS_RANK_REWORD(pUser, ACK_NOT_EXIST_REWARD);
		return;
	}

	bool bReward = true;
	::NoSQL::Command::Redis clsRedis;
	clsRedis._eServer = ::NoSQL::Command::BaseInfo::REDIS;
	clsRedis._eAction = ::NoSQL::Command::Redis::SETNX;
	std::stringstream stream;
	stream << pUser->GetUserSeq();
	clsRedis._key = stream.str();
	clsRedis._value = "1";

	::NoSQL::Command::Auto clsAuto(::NoSQL::GETCOMMANDPOOL());
	clsAuto->SetRedis(clsRedis);

	::NoSQL::SESSION()->SyncExecute(static_cast<int>(RedisServerType::WORLDBOSS_MASTER), clsAuto);

	if (0 == clsAuto->GetResult())
	{
		DEBUGLOG("%s clsAuto->GetResult() : %d GID : %d", clsAuto->GetResult(), __FUNCTION__);
		bReward = false;
	}
	else
	{
		DEBUGLOG("%s clsAuto->GetResult() : %d GID : %d", clsAuto->GetResult(), __FUNCTION__);
		SendAck_CS_REQ_WORLD_BOSS_RANK_REWORD(pUser, ACK_WORLDBOSS_AREAY_REWARD);
		return;
	}

	// 이미 보상을 받은 경우
	if (true == bReward)
	{
		DEBUGLOG("%s false == pEventTimeResource->IsWorldBossReward(pUser)", __FUNCTION__);
		SendAck_CS_REQ_WORLD_BOSS_RANK_REWORD(pUser, ACK_WORLDBOSS_AREAY_REWARD);
		return;
	}

	// 여기서 보상을 지급.....

	NResource::WorldBossReward::Data & clsRewardData = NResource::Static::Instance().GetWorldBossRewardResource()->GetData(rank + 1);

	if (0 == clsRewardData.index)
	{
		DEBUGLOG("%s 0 == clsData.index", __FUNCTION__);
		SendAck_CS_REQ_WORLD_BOSS_RANK_REWORD(pUser, ACK_NOT_EXIST_REWARD);
		return;
	}

	send_main_Reward1(pUser, clsRewardData);
	send_main_Reward2(pUser, clsRewardData);

	SendAck_CS_REQ_WORLD_BOSS_RANK_REWORD(pUser, ACK_OK);

}

} /*NCommand*/ } /*ProjectZ*/


